package cn.rongdao.jrkabao.ui.remind;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.ui.remind.event.MoXieSuccessByExportEvent;
import cn.rongdao.jrkabao.ui.remind.event.SaveCardEvent;
import cn.rongdao.jrkabao.util.moxie.MoXieManager;

/**
 * Created by jack on 2017/11/23
 */

public class AddRemindCreditCardByMailActivity extends BaseAutoFinishActivity {

    public static void start(Context context) {
        FRStartActivity.start(context, AddRemindCreditCardByMailActivity.class);
    }

    @BindView(R.id.arccbm_titlebar)
    TitleBarUIComponent titleBarUIComponent;

    private MoXieManager moXieManager;

    @Override
    protected int layoutRes() {
        return R.layout.activity_remind_credit_card_by_mail;
    }

    @Override
    protected void initView() {
        super.initView();
        initTitleBar();
        moXieManager = new MoXieManager(this);
        moXieManager.initKaMaoMoXie();
        initListener();
    }

    private void initListener() {
        moXieManager.setOnMoXieReturnListener(new MoXieManager.OnMoXieReturnListener() {
            @Override
            public void looperMethod(String email) {
                //TODO 轮询
            }

            @Override
            public void successReturn(String email) {
                //TODO 做一些成功的事情
                moXieManager.onStop();
                EventBus.getDefault().post(new MoXieSuccessByExportEvent(email));
                moXieManager = null;
                finish();
            }
        });
    }

    private void initTitleBar() {
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary));
        titleBarUIComponent.initTitle("添加信用卡");
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void saveCardMessage(SaveCardEvent saveCardEvent) {
        finish();
    }

    @OnClick({R.id.arccbm_tv_import_mail, R.id.arccbm_tv_import_auto})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.arccbm_tv_import_mail:
                moXieManager.startMoXieWithEmail();
                break;
            case R.id.arccbm_tv_import_auto:
                AddCreditCardRemindActivity.start(thisActivity(), "");
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        moXieManager = null;
    }
}
